<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'nickname',
        'mobile',
        'password',
        'avatar',
        'gender',
        'provider',
        'openid',
        'unionid',
        'state',
        'integral',
    ];

    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'openid',
        'unionid',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function getStatusAttribute()
    {
        return $this->attributes['state'];
    }

    // 给用户增加积分
    public function incIntegral($integral)
    {
        $this->increment('integral',$integral);
        $this->save();
        return $this;
    }

    // 扣除用户积分
    public function decIntegral($integral)
    {
        $this->decrement('integral',$integral);
        $this->save();
        return $this;
    }
}
